package com.thon.dao.user.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.thon.dao.user.CouponDao;
import com.thon.dao.util.AbstractDao;
import com.thon.dao.util.Page;
import com.thon.entity.user.Coupon;
@Repository
public class CouponDaoImpl extends AbstractDao<Coupon> implements CouponDao {

		public CouponDaoImpl(){
			super(Coupon.class);
		}

		@Override
		public Page<Coupon> listUnusedCoupon(int uid, Page<Coupon> page) {
			String queryString = "FROM Coupon c WHERE c.user.id = ? AND (c.couponStatus = ? ) ORDER BY c.createdDate Desc";
			return findPage(page, queryString, uid,Coupon.STATUS_UNUSE);
		}
		
		@Override
		public Page<Coupon> listUsedCoupon(Page<Coupon> page,int uid) {
			String queryString = "FROM Coupon c WHERE c.user.id = ? AND (c.couponStatus = ? OR c.couponStatus = ? ) ORDER BY c.createdDate Desc";
			return findPage(page, queryString, uid,Coupon.STATUS_USED,Coupon.STATUS_DISCARD);
		}

		@Override
		public Coupon getCoupon(int id, String no) {
			String queryString = "FROM Coupon c WHERE c.user.id = ? "
					+ "and c.couponNo = ? ";
			return findOne(queryString,null, id, no);
		}

		@Override
		public List<Coupon> listAll() {
			String queryString = "FROM Coupon ORDER BY createdDate Desc";
			return findList(queryString, null);
		}
		
		//根据优惠券状态查找
		@Override
		public Page<Coupon> findCouponByStatus(Page<Coupon> page, String value, int id) {
			String queryString = "FROM Coupon c WHERE c.user.id = ? AND  c.couponStatus = ? ORDER BY c.createdDate Desc";
			return findPage(page, queryString, id, value);
		}

		//根据优惠券类型查找
		@Override
		public Page<Coupon> findCouponByType(Page<Coupon> page, String type, int id) {
			String queryString = "FROM Coupon c WHERE c.user.id = ? AND  c.label = ? ORDER BY c.createdDate Desc";
			return findPage(page, queryString, id, type);
		}

		@Override
		public Page<Coupon> findCouponByTime(Page<Coupon> page, String overdueDt, int id) {
			DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
			String date=fmt.format(overdueDt); 
			String queryString = "FROM Coupon c WHERE c.user.id = ? "
					+ "AND WHERE c.overdueDate <= ? ORDER BY c.createdDate Desc";
			return findPage(page, queryString, id, date);
		}

		@Override
		public Page<Coupon> findCouponByCreateDt(Page<Coupon> page,
				String createDt, int id) {
			DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date=fmt.format(createDt);
			String queryString = "FROM Coupon c WHERE c.user.id = ? AND  c.createdDate >= ? ORDER BY c.createdDate Desc";
			return findPage(page, queryString, id, date);
		}

		@Override
		public Page<Coupon> findCouponPages(Page<Coupon> page) {
			String queryString = "FROM Coupon ORDER BY createdDate Desc";
			return findPage(page, queryString);
		}

		@Override
		public Page<Coupon> findCouponByProjectId(Page<Coupon> page,int projectId, int id) {
			String queryString = "FROM Coupon c WHERE c.user.id = ?  AND c.projectId = ? ORDER BY c.createdDate Desc";
			return findPage(page, queryString, id,projectId);
		}

		@Override
		public Page<Coupon> findAllCoupons(Page<Coupon> page, int uid) {
			String queryString = "FROM Coupon c WHERE c.user.id = ? ORDER BY c.createdDate Desc";
			return findPage(page, queryString, uid);
		}

		@Override
		public Long countUnusedCoupon(Integer uid) {
			String queryString  = "SELECT COUNT(*) FROM Coupon WHERE user.id = ? AND (couponStatus = ?)";
			return count(queryString, uid,Coupon.STATUS_UNUSE);
		}

		@Override
		public Page<Coupon> findAllCoupons(Page<Coupon>page) {
			String queryString = "FROM Coupon ORDER BY createdDate Desc";
			return findPage(page, queryString);
		}

		@Override
		public Page<Coupon> findCouponByStatus(Page<Coupon> page, String status) {
			String queryString = "FROM Coupon WHERE couponStatus = ? ORDER BY createdDate DESC";
			return findPage(page, queryString, status);
		}

		@Override
		public Page<Coupon> findCouponByType(Page<Coupon> page, String type) {
			String queryString = "FROM Coupon WHERE label = ? ORDER BY createdDate DESC";
			return findPage(page, queryString, type);
		}

		@Override
		public List<Coupon> listByStatus(String status) {
			String queryString = "FROM Coupon WHERE couponStatus = ? ORDER BY createdDate Desc";
			return findList(queryString, null, status);
		}
		
		@Override
		public Page<Coupon> findCouponByProject(Page<Coupon> page, int projectId) {
			String queryString = "FROM Coupon WHERE project.id = ? ORDER BY createdDate DESC";
			return findPage(page, queryString, projectId);
		}

		@Override
		public Coupon getCouponByNo(String no) {
			String queryString = "FROM Coupon WHERE couponNo = ? ";
			return findOne(queryString, null, no);
		}

		@Override
		public Page<Coupon> findCouponByProject(Page<Coupon>page,String by, String value, int ProjectId) {
			String queryString = "FROM Coupon WHERE "+by+" = ? AND project.id = ? ORDER BY createdDate DESC";
			return findPage(page, queryString, value,ProjectId);
		}

		@Override
		public Page<Coupon> findCouponByValue(Page<Coupon> page, String by, String value) {
			String queryString = "FROM Coupon WHERE "+by+" = ? ORDER BY createdDate DESC";
			return findPage(page, queryString, value);
		}

		@Override
		public Page<Coupon> findCouponByProjectId(Page<Coupon> page, int ProjectId) {
			String queryString = "FROM Coupon WHERE project.id = ? ORDER BY createdDate DESC";
			return findPage(page, queryString, ProjectId);
		}
		

}
